<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="treeNode.title" :visible="showDialog" @close="closeDialog">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-10个字符" />
      </el-form-item>
      <el-form-item label="负责人编码" prop="headid">
        <el-select v-model="formData.headid" style="width:80%" filterable placeholder="请选择">
          <el-option
            v-for="item in userlist"
            :key="item.studentid"
            :label="item.studentname"
            :value="item.studentid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="closeDialog">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { addStructure, editStructure } from '@/api/structure'
import { getAllUser } from '@/api/user'

export default {
  // props可以用数组来接收数据 也可以用对象来接收
  // props: {   props属性: {  配置选项 }  }
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    // 当前操作的节点
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      userlist: [],
      // 定义表单数据
      formData: {
        name: '', // 部门名称
        headid: '', // 部门管理
        introduce: '', // 部门介绍
        id: ''
      },
      // 定义校验规则
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 10, message: '部门名称要求1-10个字符', trigger: 'blur' }],
        headid: [{ required: true, message: '负责人id不能为空', trigger: 'blur' },
          { min: 8, max: 16, message: '负责人id要求8-16个字符', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' }]
      }
    }
  },
  async created() {
    const { rows } = await getAllUser(1, 999)
    this.userlist = rows
  },
  methods: {
    btnOk() {
      this.$refs.deptForm.validate(async isOK => {
        if (isOK) {
          var data = {
            name: this.formData.name, // 部门名称
            headid: this.formData.headid, // 部门管理
            introduce: this.formData.introduce, // 部门介绍
            pid: ''
          }
          if (this.treeNode.title === '添加部门') {
            data.pid = this.treeNode.id.toString()
            await addStructure(data) // 调用新增接口
          } else {
            data.pid = this.treeNode.pid.toString()
            var id = this.treeNode.id.toString()
            await editStructure(id, data) // 调用新增接口
          }
          this.$emit('addDepts')
          this.$emit('closepop')
        }
      })
    },
    closeDialog() {
      this.$refs.deptForm.resetFields()
      this.$emit('closepop')
    },
    updateFormData(data) {
      this.formData = data
    }
  }
}
</script>
